<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo</title>
    <script type="text/javascript" src="../vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="search" />

        <ul>
            <li v-for="item in newPerson" :key="item.id">
                {{item.name}}--{{item.age}}
            </li>
        </ul>
    </div>
</body>
<script type="text/javascript">
    Vue.config.productionTip = false
    const vm = new Vue({
        el: "#app",
        data() {
            return {
                search: '',
                person: [
                    { id: "001", name: "小乔", age: 18 },
                    { id: "002", name: "大乔", age: 12 },
                    { id: "003", name: "貂蝉", age: 33 },
                    { id: "004", name: "上官婉儿", age: 6 },

                ],
                // 设置一个空数组保存监听到的值
                newPerson: [],
            }
        },
        watch: {
            search: {
                immediate: true,
                handler(newValue) {
                    this.newPerson = this.person.filter((item) => {
                        return item.name.includes(newValue)
                    })
                }
            }
        }
    })
</script>

</html>